package com.megoc.menu.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.megoc.menu.R;
import com.megoc.menu.model.find.FindList;

import java.util.List;

/**
 * Created by Megoc on 2015/10/20.
 */
public class FindListViewAdapter extends BaseAdapter{

    private List<FindList> findLists;
    private Context context;

    public FindListViewAdapter(List<FindList> findLists, Context context) {
        this.findLists = findLists;
        this.context = context;
    }

    @Override
    public int getCount() {
        return findLists.size();
    }

    @Override
    public Object getItem(int position) {
        return findLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_find_listview,null);
            viewHolder=new ViewHolder();
            viewHolder.headView= (SimpleDraweeView) convertView.findViewById(R.id.head_shop_img);
            viewHolder.shopTitleView= (TextView) convertView.findViewById(R.id.head_title);
            viewHolder.shopNameView= (TextView) convertView.findViewById(R.id.head_shop_name);
            viewHolder.buttomView= (SimpleDraweeView) convertView.findViewById(R.id.buttom_shop_bigimg);
            viewHolder.imageView= (ImageView) convertView.findViewById(R.id.like_animation);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        FindList findList=findLists.get(position);
        viewHolder.headView.setImageURI(Uri.parse(findList.getStoreLogoUrl()));
        viewHolder.shopTitleView.setText(findList.getStoreTitle());
        viewHolder.shopNameView.setText(findList.getUserName());
        viewHolder.buttomView.setImageURI(Uri.parse(findList.getCoverUrl()));
        viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AnimationDrawable animationDrawable = (AnimationDrawable) viewHolder.imageView.getDrawable();

                animationDrawable.start();
            }
        });
        return convertView;
    }
    class ViewHolder{
        SimpleDraweeView headView;
        TextView shopTitleView;
        TextView shopNameView;
        SimpleDraweeView buttomView;
        ImageView imageView;
    }
}
